import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './vuex/store'
import axios from 'axios'
import './plugins/ant'
Vue.config.productionTip = false
// 配置请求的跟路径
axios.defaults.baseURL = 'http://47.111.244.163:8086/'
Vue.prototype.$http = axios
router.beforeEach((to, from, next) => {
  // console.log(to.matched)
  store.commit('clearMenus')
  to.matched.forEach(i => {
    store.commit('addMenus', i.name)
  })

  if (to.path !== '/login') {
    // 对路由进行验证
    if (store.state.isLogin === '100') { // 已经登陆
      // 设置标题
      if (to.path === '/dashboard/userList') {
        store.commit('changeTitle', '用户列表')
        store.commit('changeCurrent', 'userList')
      } else if (to.path === '/dashboard/userDetail') {
        if (store.state.editUserInfo.name === undefined) {
        }
        store.commit('changeTitle', '用户详情')
        store.commit('changeCurrent', 'userDetail')
      } else if (to.path === '/dashboard/userEdit') {
        // console.log(store.state.editUserInfo.name === undefined)
        if (store.state.editUserInfo.name === undefined) {
          next('/')
        } else {
          store.commit('changeTitle', '用户编辑')
          store.commit('changeCurrent', 'userEdit')
        }
      }
      next() // 正常跳转
    } else {
      // 未登录则跳转到登陆界面,to.path表示把当前路由信息传过去，方便登录成功后跳转
      next('/login?redirect=' + to.path)
    }
  } else {
    next()
  }
})
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
